package com.dkf.springcloud;

import com.dkf.springcloud.bean.User;
import lombok.extern.slf4j.Slf4j;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.UUID;

@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
public class SendTester {
    @Autowired
    AmqpTemplate amqpTemplate;
    @Autowired
    RabbitTemplate rabbitTemplate;
    public static final String ONE_NAME_1="one_name_1";
    public static final String THREE="Queue3";
    @Test
    @Ignore
    public void send1(){
        for (int i = 0; i < 10; i++) {
            rabbitTemplate.convertAndSend(ONE_NAME_1,"this is first message from java  aa");
        }
    }

    @Test
    public void send2(){
       // rabbitTemplate.convertAndSend("aaaaa.weather","this is first message from java  aa");
        rabbitTemplate.convertAndSend("exchange_2", "aaaaa.weather", "Hello from RabbitMQ!");
    }

    @Test
    public void send3(){

        User user = new User();
        user.setName("name");
        user.setPassword("123");
        user.setAddress("aaaaa");
        rabbitTemplate.setConfirmCallback((data, ack, cause) -> {
            if (!ack) {
                System.out.println("消息发送失败!" + cause + data.toString());
            } else {
                System.out.println("消息发送成功,消息ID：" + (data != null ? data.getId() : null));
            }
        });
        rabbitTemplate.setReturnCallback((Message message, int replyCode, String replyText, String exchange, String routingKey)->{
            log.info("returnedMessage ===> replyCode={} ,replyText={} ,exchange={} ,routingKey={}", replyCode, replyText, exchange, routingKey);
            System.out.println("=============returnCallback触发。消息路由到queue失败===========");
            System.out.println("msg="+new String(message.getBody()));
            System.out.println("replyCode="+replyCode);
            System.out.println("replyText="+replyText);
            System.out.println("exchange="+exchange);
            System.out.println("routingKey="+routingKey);
        });
        CorrelationData correlationData = new CorrelationData();
        correlationData.setId(UUID.randomUUID().toString());
        //rabbitTemplate.convertAndSend("exchange_2", "aaaaa.weather", user,correlationData);
        rabbitTemplate.convertAndSend("exchange_2", "aaaaa", user,correlationData);

    }


}
